package demo.rao.mideng.core;

import demo.rao.mideng.annotation.RaoMiDeng;
import lombok.ToString;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/**
 * 借鉴com.alibaba.druid.support.spring.stat.annotation.StatAnnotationAdvisor
 */
@ToString
public class MiDengAnnoAdvisor extends AbstractPointcutAdvisor {
    private MiDengInterceptor miDengInterceptor;
    private Advice advice;
    private Pointcut pointcut;

    public MiDengAnnoAdvisor(MiDengInterceptor miDengInterceptor) {
        this.miDengInterceptor = miDengInterceptor;
        this.advice = this.buildAdvice();
        this.pointcut = this.buildPointcut();

    }

    public Pointcut getPointcut() {
        return this.pointcut;
    }

    public Advice getAdvice() {
        return this.advice;
    }

    protected Advice buildAdvice() {
        return this.miDengInterceptor;
    }

    protected Pointcut buildPointcut() {
        //如果类上有@RaoMiDeng，这个类上所有的方法都会被进行代理，如果类上没有而方法上面有，这个方法就会被代理
        Pointcut cpc = new AnnotationMatchingPointcut(RaoMiDeng.class, true);
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(RaoMiDeng.class);
        ComposablePointcut result = (new ComposablePointcut(cpc)).union(mpc);
        return result;
    }
}
